window.userName = $('._buryDot-userEmail')[0] ? $('._buryDot-userEmail').text() : '游客';
$(function($) {
	//模拟请求
	$('._tools_http').click(function() {
		$("#_json_text").html('');
		var url = $('._request_url').attr('data-url');
		var method = $('._request_url').attr('data-method');
		var param_string = "";
		//拼接url的后续参数
		$("._api_request_params").each(function() {
			var that = $(this);
			var param_name = that.attr('data-name');
			var param_value = that.val();

			if(param_value != '') {
				if(param_name == 'size' && param_value > 50) {
					param_string += "&" + param_name + "=50";
				} else {
					param_string += "&" + param_name + "=" + param_value;
				}
			}
		});
		param_string = param_string.substr(1);
		param_string = param_string.replace(' ', '');
		//请求详情地址回写
		$("._api_http_content").text(url + "?" + param_string);
		var option = {
			url: url,
			param_string: param_string,
			method: method,
			api_id:$('._api_id').val()
		};
		//按钮样式改变
		var cur = $(this);
		buttonIng(cur,'请求中...')
		
		$.ajax({
			url: '/Index/ajaxToolshttpproxy.html',
			type: 'POST',
			dataType: 'json',
			async: true,
			data: option,
			success: function(req) {
				cur.html('模拟请求').removeAttr('disabled');
				if(req.status == 200) {
					//json返回示例回写
					var json_string = jsonFormat(req.data, false);	
					$("#_json_text").html(json_string);
					//JSON代码样式	
				}else if(req.status == 1005){
					$('._loginBox').modal('show');
				}else {
					var tab = '    ';
					var mstring = '{\n' + tab + '"return_code":' + req.status + ',\n' + tab + '"return_msg":' + req.msg + '\n}';
					$("#_json_text").html(mstring);
				}
				$('#_json_text').each(function(i,block){
					hljs.highlightBlock(block);
				});			
				return false;
			},
			error: function() {
				//按钮样式重置
				cur.html('模拟请求').removeAttr('disabled');
				toastr.error('系统异常',null,opts);
			}
		});

	});
	//埋点
	$(document).on('click', '#_tools_http', function() {
		var obj = [];
		var requParams = $('._reqParamter tr')
		for(var i = 0, len = requParams.length; i < len; i++) {
			var requParam = $(requParams[i]).find('td')
			var l = requParam.length
			if($(requParam[0]).text() == 'page' || $(requParam[0]).text() == 'size') {} else {
				obj["'" + $(requParam[0]).text() + "'"] = $(requParam[l - 1]).find('input').val() || '未赋值'
			}
		}
		//todo 埋点
		zhuge.track('调试_模拟请求', {
			'用户':userName,
			'请求内容': obj
		})
	})
});